import type { UserState } from "./types";

export const getters = {
	// 是否已登录
	isLoggedIn: (state: UserState): boolean => {
		return !!state.token;
	},

	// 获取用户名
	username: (state: UserState): string => {
		return state.userInfo?.username || "";
	},

	// 获取用户昵称
	nickname: (state: UserState): string => {
		return state.userInfo?.nickname || "";
	},

	// 获取用户头像
	avatar: (state: UserState): string => {
		return state.userInfo?.avatar || "";
	},

	// 是否有指定权限
	hasPermission:
		(state: UserState) =>
		(permission: string): boolean => {
			return state.permissions.includes(permission);
		},

	// 是否有指定角色
	hasRole:
		(state: UserState) =>
		(role: string): boolean => {
			return state.roles.includes(role);
		},
};
